package serverImp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.NewsDao;
import dao.UserDao;
import daoimp.NewsDaoImp;
import daoimp.UserDaoImp;
import entity.News;
import server.NewsServer;
import transaction.Transaction;
import transactionimpl.TransactionImpl;

public class NewsServerImp implements NewsServer
{
    NewsDao dao = null;
    Transaction trans = null;
    public NewsServerImp()
    {
        dao = new NewsDaoImp();
        trans = new TransactionImpl();
    }
    public int add(News a)
    {
        int result=-1;
        try
        {
            trans.beginTransaction();
            result= dao.add(a);
            trans.commit();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            trans.rollback();
        }
        return result;
    }

    public int delete(int aid)
    {
        int result=-1;
        try
        {
            trans.beginTransaction();
            result= dao.delete(aid);
            trans.commit();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            trans.rollback();
        }
        return result;
    }

    public int update(News a)
    {
        int result=-1;
        try
        {
            trans.beginTransaction();
            result= dao.update(a);
            trans.commit();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            trans.rollback();
        }
        return result;
    }

    public List query()
    {
        List list=new ArrayList();
        try
        {
            trans.beginTransaction();
            list=dao.query();
            trans.commit();
        }
//        catch (SQLException e)
        catch (Exception e)
        {
            e.printStackTrace();
            trans.rollback();
        }
        return list;
    }
    public News selectId(int id)
    {
        News p = new News();
        try
        {
            trans.beginTransaction();
            p = dao.selectId(id);
            trans.commit();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            trans.rollback();
        }
        return p;
    }
/*    public static void main(String[] args) {
		 NewsServer ss=new NewsServerImp();
		 ss.add(new News("sdf", "sdf", "sdgdgd"));
		 
		 
		 List list =ss.query();
		 System.out.println(list.size());
	}*/
}
